import java.util.List;

import com.alibaba.fastjson.JSON;

import com.example.MongoDbApp;
import com.example.entity.mogodb.DemoEntity;
import com.example.repository.mongodb.DemoRepo;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author LiYuhang
 * @version 0.1
 * @date 2024/1/30 15:49
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MongoDbApp.class)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class UserRepositoryCrud {
    @Autowired
    DemoRepo demoRepo;

    @Test
    @Order(value = 1)
    public void saveDemoTest() {
        DemoEntity demoEntity = new DemoEntity();
        demoEntity.setId(System.currentTimeMillis());
        demoEntity.setTitle("Spring Boot 中使用 MongoDB");
        demoEntity.setDescription("测试数据");
        demoEntity.setBy("liyuhang");
        demoEntity.setUrl("http://www.yuwowugua.com");
        demoRepo.save(demoEntity);
    }

    @Test
    @Order(value = 2)
    public void update() {
        DemoEntity demoEntity = new DemoEntity();
        demoEntity.setId(2L);
        demoEntity.setTitle("Spring Boot 中使用 MongoDB");
        demoEntity.setDescription("测试数据 before update");
        demoEntity.setBy("liyuhang");
        demoEntity.setUrl("http://www.yuwowugua.com");
        demoRepo.save(demoEntity);
        //  mongoTemplate.insert(demoEntity);
        demoEntity.setDescription("测试数据 after update");
        demoRepo.save(demoEntity);
    }

    @Test
    @Order(value = 3)
    public void delete() {
        demoRepo.deleteById(2L);
    }

    @Test
    @Order(value = 4)
    public void list() {
        final List<DemoEntity> demoRepoAll = demoRepo.findAll();
        System.out.println(JSON.toJSONString(demoRepoAll));
    }
}
